package homework.util;

import homework.bean.ClassBean;
import homework.bean.StudentBean;

import java.io.*;
import java.util.*;

/**
 * @Author(作者) ys
 * @Description(类的描述):  提供数据的工具类
 * @Date(创建时间)
 * @version(开发的版本):1.0
 **/
public class DataUtil {
    /**
     * 存储学生对象的集合
     */
    public  static  Map<Integer, StudentBean> studentsInfo = new HashMap<Integer, StudentBean>();
    /**
     * 存储班级的集合
     */
    public static  Map<Integer, ClassBean> classesInfo=new HashMap<Integer, ClassBean>();

    //记录下一个学生的编号
    public static int nextId;

    static{
        //初始化学生的数据
        //.. 读取student.txt的数据 封装数据
        File studentFile  = new File("E:\\javaooGit\\javaooDay22\\src\\homework\\data\\student.txt");
        File classFile = new File("E:\\javaooGit\\javaooDay22\\src\\homework\\data\\classInfo.txt");
        File studentId = new File("E:\\javaooGit\\javaooDay22\\src\\homework\\data\\id.txt");

        if(!(studentFile.length() == 0)){

            try (    FileInputStream sFis = new FileInputStream(studentFile);
                    ObjectInputStream sIn = new ObjectInputStream(sFis)){
                StudentBean s;
                while( sFis.available() > 0){
                    s = (StudentBean) sIn.readObject();
                    studentsInfo.put(s.getStudentId(), s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(classFile.length() == 0){
            classesInfo.put(1, new ClassBean(1, "班级1"));
            classesInfo.put(2, new ClassBean(2, "班级2"));
        }else{
            try (   FileInputStream cfis = new FileInputStream(classFile);
                    ObjectInputStream cIn = new ObjectInputStream(cfis);
                    BufferedReader idRead = new BufferedReader(new FileReader(studentId))
                    ){
                ClassBean c;
                while(cfis.available() > 0){
                    c = (ClassBean) cIn.readObject();
                    classesInfo.put(c.getClassId(), c);
                }
                nextId = Integer.parseInt(idRead.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //保存下一个学生的编号
        //nextId = studentsInfo.size()+1;
       //读取classinfo.txt 封装数据
    }

    private   DataUtil(){

    }

    /**
     * 查询所有的学生
     * @return  list类型的学生集合的对象
     */
    public static List<StudentBean> getAllStudents(){
        //创建用来保存学生对象的集合
        List<StudentBean> list = new ArrayList<StudentBean>();
        //从 学生的map集合中遍历所有的学生对象 并把每个学生保存到list集合中
          Set<Integer> keys =    studentsInfo.keySet();
        for (Integer key : keys) {
            StudentBean studentBean = studentsInfo.get(key);
            list.add(studentBean);
        }
        return list;
    }
    /**
     * 查询所有的班级
     * @return  班级对象的集合
     */
    public static List<ClassBean> getAllClasses(){
        //创建用来保存学生对象的集合
        List<ClassBean> list = new ArrayList<ClassBean>();
        //从 学生的map集合中遍历所有的学生对象 并把每个学生保存到list集合中
        Set<Integer> keys =    classesInfo.keySet();
        for (Integer key : keys) {
            ClassBean classBean = classesInfo.get(key);
            list.add(classBean);
        }
        return list;
    }

    /**
     *
     * @param studentBean 学生对象
     */
    public static void  addStudent(StudentBean studentBean){
        studentsInfo.put(studentBean.getStudentId(),studentBean);
        //记录下一个学生的编号
         nextId++;
    }

    /**
     * 删除
     * @param studentId
     */
    public static void  deleteStudent(Integer studentId){

        studentsInfo.remove(studentId);
    }

    /**
     * 修改
     * @param studentBean
     */
    public static  void  updateStudent(StudentBean studentBean){

        studentsInfo.put(studentBean.getStudentId(),studentBean);
    }
    /**
     * 根据编号来查询学生
     * @param studentId 学生编号
     * @return  该编号的学生对象
     */
    public static StudentBean getStudentById(Integer studentId){

        StudentBean studentBean = studentsInfo.get(studentId);

        return studentBean;
    }

    /**
     * 将学生和班级对象序列化到文件中
     */
    public static void serializableStudentAndClass(){
        if(studentsInfo.size() != 0){
            File studentInfo = new File("E:\\javaooGit\\javaooDay22\\src\\homework\\data\\student.txt");
            File studentId = new File("E:\\javaooGit\\javaooDay22\\src\\homework\\data\\id.txt");

            try( ObjectOutputStream sOut = new ObjectOutputStream(new FileOutputStream(studentInfo));
                BufferedWriter idWriter = new BufferedWriter(new FileWriter(studentId))
            ) {

                Set<Integer> integers = studentsInfo.keySet();
                for (Integer id : integers) {
                    studentsInfo.get(id).setPicture(savePicture(studentsInfo.get(id).getPicture()));
                    sOut.writeObject(studentsInfo.get(id));
                }
                idWriter.write(nextId+"");
                sOut.flush();
                idWriter.flush();
            }catch (Exception e){

            }
        }

        if(classesInfo.size() != 0 ){
            File classInfo = new File("E:\\javaooGit\\javaooDay22\\src\\homework\\data\\classInfo.txt");
            try(ObjectOutputStream cOut = new ObjectOutputStream(new FileOutputStream(classInfo))){
                Set<Integer> classes = classesInfo.keySet();
                for (Integer id : classes) {
                    cOut.writeObject(classesInfo.get(id));
                }
                cOut.flush();
            }catch(Exception e){
                e.printStackTrace();
            }
        }


    }

    /**
     * 保存用户上传的照片
     * @param filePath 上传照片的路径
     * @return 保存照片的路径
     */
    public static String  savePicture(String filePath){

        File file = new File(filePath);
        String path = "E:\\javaooGit\\javaooDay22\\photo"+
                File.separator + System.currentTimeMillis()
                + filePath.substring(filePath.lastIndexOf("."));
        File picFile = new File(path);

        try(
                InputStream in = new FileInputStream(file);
                OutputStream out = new FileOutputStream(picFile);
                ){
            int a = 0;
            while(a != -1){
                a = in.read();
                out.write((char)a);
            }
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }

        return path;
    }
}